import 'package:flutter/material.dart';
import 'package:sample2/components/alert_helper.dart';

class HomeContent3 extends StatelessWidget {
  const HomeContent3({super.key});
  @override
  Widget build(BuildContext context) {
    return Container(
        constraints: const BoxConstraints.expand(),
        child: Column(
          children: const [
            Text("Flow"),
            Expanded(
              child: FlowMenu(),
            )
          ],
        ));
  }
}

class FlowMenu extends StatefulWidget {
  const FlowMenu({super.key});

  @override
  State<StatefulWidget> createState() {
    return _FlowMenuState();
  }
}

class _FlowMenuState extends State<FlowMenu>
    with SingleTickerProviderStateMixin {
  late AnimationController menuAcnimation;
  IconData lastTapped = Icons.notifications;
  final List<IconData> menuItems = <IconData>[
    Icons.home,
    Icons.new_releases,
    Icons.notifications,
    Icons.settings,
    Icons.menu
  ];
  void _updateMenu(IconData icon) {
    if (icon != Icons.menu) {
      setState(() {
        lastTapped = icon;
      });
    }
  }

  @override
  void initState() {
    menuAcnimation = AnimationController(
        duration: const Duration(milliseconds: 250), vsync: this);
    super.initState();
  }

  Widget flowMenuItem(IconData icon, int idx) {
    final double buttonDiameter =
        MediaQuery.of(context).size.width / menuItems.length;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: RawMaterialButton(
        fillColor: lastTapped == icon ? Colors.amber[700] : Colors.blue,
        splashColor: Colors.amber[100],
        shape: const CircleBorder(),
        constraints: BoxConstraints.tight(Size(buttonDiameter, buttonDiameter)),
        onPressed: () {
          _updateMenu(icon);
          if (icon != Icons.menu) {
            AlertHelper.showAlert(
                context: context, message: "Click Item at index $idx");
          }
          menuAcnimation.status == AnimationStatus.completed
              ? menuAcnimation.reverse()
              : menuAcnimation.forward();
        },
        child: Icon(
          icon,
          color: Colors.white,
          size: 45.0,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Flow(
      delegate: FlowMenuDelegate(menuAnimation: menuAcnimation),
      children: menuItems.map((IconData icon) {
        int idx = menuItems.indexOf(icon);
        return flowMenuItem(icon, idx);
      }).toList(),
    );
  }
}

class FlowMenuDelegate extends FlowDelegate {
  FlowMenuDelegate({required this.menuAnimation})
      : super(repaint: menuAnimation);
  final Animation<double> menuAnimation;

  @override
  bool shouldRepaint(covariant FlowMenuDelegate oldDelegate) {
    return menuAnimation != oldDelegate.menuAnimation;
  }

  @override
  void paintChildren(FlowPaintingContext context) {
    double dx = 0.0;
    for (int i = 0; i < context.childCount; ++i) {
      dx = context.getChildSize(i)!.width * i;
      context.paintChild(i,
          transform: Matrix4.translationValues(dx * menuAnimation.value, 0, 0));
    }
  }
}
